package com.childmiles.gwt.client.ui;

import com.childmiles.gwt.client.AwardWinnerService;
import com.childmiles.gwt.client.AwardWinnerServiceAsync;
import com.childmiles.gwt.client.domain.AwardWinner;
import com.google.gwt.app.place.AbstractActivity;
import com.google.gwt.app.place.Activity;
import com.google.gwt.app.place.ActivityManager;
import com.google.gwt.app.place.ActivityMapper;
import com.google.gwt.app.place.Place;
import com.google.gwt.app.place.PlaceController;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Widget;

public class MainActivity extends AbstractActivity {

	public interface MainDisplay extends Display {
	}
	
	private static final Place SETTINGS_PLACE = new StringPlace("Settings");
	private static final Place AWARDS_PLACE = new StringPlace("Awards");
    /**
     * Create a remote service proxy to talk to the server-side service.
     */
    private final AwardWinnerServiceAsync awardWinnerService = GWT.create(AwardWinnerService.class);

	private final EventBus eventBus = new HandlerManager(null);
	private final PlaceController placeController = new PlaceController(eventBus);
	private final ActivityMapper mapper = new ActivityMapper() {
		public Activity getActivity(Place place) {
			if (place != null) {
				mainDisplay.showWidget(1);
				if (place.equals(SETTINGS_PLACE)) {
					mainDisplay.showWidget(2);
				} else if (place.equals(AWARDS_PLACE)) {
				}
			} else {
				mainDisplay.showWidget(0);
			}
			return MainActivity.this;
		}
	};
    private final ActivityManager manager = new ActivityManager(mapper, eventBus);
	
    private AwardWinner currentAwardWinner;

	private final MainDisplayImpl mainDisplay;

    public MainActivity(MainDisplayImpl mainDisplay) {
		this.mainDisplay = mainDisplay;
		manager.setDisplay(mainDisplay);
		mainDisplay.setActivity(this);
	}

	public void goBack() {
		placeController.goTo(AWARDS_PLACE);
	}
    
	public void start(Display panel, EventBus eventBus) {
		//manager.setDisplay(mainDisplay);
		panel.showActivityWidget(mainDisplay);
	}

	public void onClick(int rowIndex, int colIndex, Widget widget) {
        if (rowIndex == 0 && colIndex == 0) {
            //for AwardWinnerImage go to settings screen
        	placeController.goTo(SETTINGS_PLACE);
            return;
        }
        AwardPanel awardPanel = (AwardPanel) widget;
        EditAwardsDialog editAwardsDialog = new EditAwardsDialog(awardWinnerService, currentAwardWinner, awardPanel);
        editAwardsDialog.addStyleName("editAwardsDialog");
        editAwardsDialog.show();
	}

	public void saveAwardWinnerPhoto(String text) {
		currentAwardWinner.setPhotoUrl(text);
	}
	
	public void loginAsAwardWinner(final String awardWinnerName) {
        awardWinnerService.find(awardWinnerName,new AsyncCallback<AwardWinner>() {
            public void onFailure(Throwable caught) {
                throw new RuntimeException(caught);
            }
            public void onSuccess(AwardWinner result) {
                if (result == null) { //not found
                    currentAwardWinner = new AwardWinner(awardWinnerName);
                }
                else {
                    currentAwardWinner = result;
                }
                mainDisplay.showAwards(currentAwardWinner);
                mainDisplay.showWidget(1);
            }
        });
	}

}
